<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class Company extends Model
{
    // 表名
    protected $name = 'company';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'goods_showtype_text',
        'status_text',
        'is_vip_text',
        'creator_text',
        'industry_text'
    ];

    protected static function init(){
        self::afterInsert(function($row){
            $oldCompanyId = \think\Db::name('user')->where('id',$row['creator'])->value('company_id');
            if(empty($oldCompanyId)){ //创建团队者
                \think\Db::name('user')->where('id',$row['creator'])->update(['company_id'=> $row['id']]);
            }
            \think\Db::name('user')->where('id',$row['creator'])->update(['selfcompany_id' => $row['id']]);
        });


    }

    public static function getList($status = 1){
        $data = \think\Db::name('company')->where(function($query) use ($status){
            if($status != 'all'){
                $query->where('status',$status);
            }
        })->select();
        return $data;
    }
    
    public function getCreatorList(){
        $model = new User();
        $list = Common::getList($model,'nickname');
        return $list;
    }

    public function getCreatorTextAttr($value,$data){
        $value = $value ? $value : (isset($data['creator']) ? $data['creator'] : '');
        $list = $this->getCreatorList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    
    public function getGoodsShowtypeList()
    {
        return ['0' => __('Goods_showtype 0'),'1' => __('Goods_showtype 1')];
    }     

    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1'),'2' => __('Status 2')];
    }     

    public function getIsVipList()
    {
        return ['0' => __('Is_vip 0'),'1' => __('Is_vip 1')];
    }

    public function getIndustryList(){
        return [
            '0' => '互联网/信息技术',
            '1' => '制造业',
            '2' => '贸易/批发/零售',
            '3' => '房地产业',
            '4' => '建筑业',
            '5' => '金融业',
            '6' => '服务业',
            '7' => '运输/物流/仓储',
            '8' => '教育行业',
            '9' => '文体/娱乐/传媒',
            '10' => '商业服务/租赁',
            '11' => '医疗医药',
            '12' => '政府/事业单位',
            '13' => '社会组织',
            '14' => '科研服务',
            '15' => '公共/环境',
            '16' => '居民服务',
            '17' => '开采业',
            '18' => '农/林/牧/渔',
            '19' => '电/热/燃气/水/供应',
            '20' => '其他行业'
        ];
    }

    public function getIndustryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['industry']) ? $data['industry'] : '');
        $list = $this->getIndustryList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGoodsShowtypeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['goods_showtype']) ? $data['goods_showtype'] : '');
        $list = $this->getGoodsShowtypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsVipTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['is_vip']) ? $data['is_vip'] : '');
        $list = $this->getIsVipList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
